package com.mecuros.auth.handler;

import com.mecuros.common.core.util.SpringContextHolder;
import com.mecuros.common.core.util.WebUtils;
import com.mecuros.common.log.event.SysLogEvent;
import com.mecuros.common.log.util.SysLogUtils;
import com.mecuros.common.security.handler.AbstractLogoutSuccessEventHandler;
import com.mecuros.demo.api.entity.SysLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.stereotype.Component;

/**
 * @author yujianbo
 * @Classname CarlosLogoutSuccessEventHandler
 * @Description 登陆日志
 * @Date 2022/4/28 8:21 下午
 */
@Slf4j
@Component
public class CarlosLogoutSuccessEventHandler extends AbstractLogoutSuccessEventHandler {
    @Override
    public void handle(Authentication authentication) {
        log.info("用户：{} 退出成功", authentication.getPrincipal());
        SecurityContextHolder.getContext().setAuthentication(authentication);

        SysLog logVo = SysLogUtils.getSysLog();
        logVo.setTitle("退出成功");
        // 发送异步日志事件
        Long startTime = System.currentTimeMillis();
        Long endTime = System.currentTimeMillis();
        logVo.setTime(endTime - startTime);

        // 设置对应的token
        WebUtils.getRequest().ifPresent(request -> logVo.setParams(request.getHeader(HttpHeaders.AUTHORIZATION)));

        // 这边设置ServiceId
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication auth2Authentication = (OAuth2Authentication) authentication;
            logVo.setServiceId(auth2Authentication.getOAuth2Request().getClientId());
        }
        logVo.setCreateBy(authentication.getName());
        logVo.setUpdateBy(authentication.getName());
        SpringContextHolder.publishEvent(new SysLogEvent(logVo));

    }
}
